package com.example.login;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.common.function.DefaultTextWatcher;
import com.example.login.impl.LoginPresenterImpl;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends AppCompatActivity implements LoginContract.LoginView {
    @BindView(R.id.tel_num)
    EditText mTelNum;
    @BindView(R.id.verify_code)
    EditText mVerifyCode;
    @BindView(R.id.get_verify_code)
    Button mGetVCode;
    @BindView(R.id.login)
    Button mLogin;

    private LoginContract.LoginPresenter mPresenter;
    /**
     * 是否在倒计时中
     */
    private boolean inCountDown = false;
    private Dialog mWaitingDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mPresenter = new LoginPresenterImpl(this);

        mTelNum.addTextChangedListener(DefaultTextWatcher.after(s -> {
                    mGetVCode.setEnabled(!inCountDown && Util.checkPhoneNum(s));
                    boolean enabled = !inCountDown
                            && Util.checkPhoneNum(s)
                            && Util.checkVerCode(mVerifyCode.getText());
                    mLogin.setEnabled(enabled);
                }
        ));
        mVerifyCode.addTextChangedListener(DefaultTextWatcher.after(s ->
                mLogin.setEnabled(Util.checkPhoneNum(mTelNum.getText()) && Util.checkVerCode(s))
        ));

        mGetVCode.setOnClickListener(v -> mPresenter.fetchVerifyCode());
        mLogin.setOnClickListener(v -> mPresenter.login(mTelNum.getText(), mVerifyCode.getText()));
    }

    @Override
    public void onCountDown(long count) {
        inCountDown = count != 0;
        mGetVCode.setEnabled(!inCountDown && Util.checkPhoneNum(mTelNum.getText()));
        if (inCountDown) {
            mGetVCode.setText(String.format(getText(R.string.count_down_format).toString(), count));
        } else {
            mGetVCode.setText(R.string.get_verify_code);
        }
    }

    @Override
    public void onLogging() {
        if(mWaitingDialog == null){
            ProgressDialog dialog = new ProgressDialog(this);
            dialog.setCancelable(false);
            dialog.setMessage("登录中...");
            mWaitingDialog = dialog;
        }
        if(mWaitingDialog.isShowing()){
            mWaitingDialog.cancel();
        }
        mWaitingDialog.show();
    }

    @Override
    public void onLoginSuccess() {
        Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
        if(mWaitingDialog != null){
            mWaitingDialog.dismiss();
        }
    }

    @Override
    public void onLoginError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        if(mWaitingDialog != null){
            mWaitingDialog.dismiss();
        }
    }
}
